package com.lagou.client;

import com.lagou.service.UserService;
import org.I0Itec.zkclient.ZkClient;

import java.util.*;

public class ClientBootStrap {

    public static  final String providerName="UserService#sayHello#";

    public static  final String parentNodeName="/provider";
//    public final static String zkAddr = "192.168.179.128:2181,192.168.179.128:2182,192.168.179.128:2183";
    public final static String zkAddr = "127.0.0.1:2181";


    public static List<Map<String, Object>> rpcConsumerList = new ArrayList<>(2);

    public static Map<String, Object> rpcMap = new HashMap<>(2);

    public static void main(String[] args) throws InterruptedException {
        ZkClient zkClient = new ZkClient(zkAddr);

        boolean provider = zkClient.exists(parentNodeName);
        if (!provider) {
            return;
        }
        List<String> childrenNodeList = zkClient.getChildren(parentNodeName);
        for (String ipObj : childrenNodeList) {
            String[] ipPortArray = ipObj.split(":");
            String ip = ipPortArray[0];
            int port = Integer.parseInt(ipPortArray[1]);
            RpcConsumer rpcConsumer1 = new RpcConsumer(ip, port);
            UserService proxy = (UserService) rpcConsumer1.createProxy(UserService.class);
            rpcMap.put(ipObj, proxy);
        }

        zkClient.subscribeChildChanges(parentNodeName, (s, list) -> {
            rpcMap.clear();
            for (String s1 : list) {
                String[] ipPortArray = s1.split(":");
                String ip = ipPortArray[0];
                int port = Integer.parseInt(ipPortArray[1]);
                RpcConsumer rpcConsumer1 = new RpcConsumer(ip, port);
                UserService proxy = (UserService) rpcConsumer1.createProxy(UserService.class);
                rpcMap.put(s1, proxy);
            }
        });

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;

        while (true){
            Iterator<String> iterator = rpcMap.keySet().iterator();
            Thread.sleep(2000);
            String getKey = null;
            while (iterator.hasNext()) {
                String key = iterator.next();
                Object readData = zkClient.readData(parentNodeName + "/" + key + "/time");
                long date = Long.parseLong(readData.toString());
                long diff = date - System.currentTimeMillis();
                long sec = diff % nd % nh % nm / ns;
                if (sec < 5) {
                    continue;
                }
                getKey= key;
            }
            try {
                Thread.sleep(2000);
                UserService proxy;
                if (getKey == null) {
                    getKey = rpcMap.keySet().iterator().next();
                    proxy = (UserService) rpcMap.get(rpcMap.keySet().iterator().next());
                } else {
                    proxy = (UserService) rpcMap.get(getKey);
                }
                System.out.println(proxy.sayHello(rpcMap.keySet().iterator().next()+" ,are you ok?"));
                zkClient.writeData(parentNodeName + "/" + getKey + "/time", System.currentTimeMillis());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }




}
